import React from "react";
import type { DataAction, IProTableRef } from "@yaf/pro-ui";
import { getDictPageList, getDictInfo, createDict, updateDict, deleteDict } from "../api";

export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>): DataAction => ({
  fetchList: (params) => {
    const { page, pageSize, filter } = params;
    return getDictPageList(Object.assign({ page, pageSize }, filter || {})).then((resp) => resp.data)
  },
  getItem: (rowKey) => {
    const dataSetId = rowKey as number;
    return getDictInfo(dataSetId).then((resp) => {
      const { dictName, dictType, status, remark } = resp.data.dictType;
      return {
        dictName: dictName,
        dictType: dictType,
        status: status,
        remark: remark,
      };
    })
  },
  createItem: (params) => {
    return createDict(params).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  updateItem: (rowKey, params) => {
    return updateDict(rowKey as number, params).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  deleteItems: (selectedRowKeys) => {
    return deleteDict(selectedRowKeys.map((item) => parseInt(item)))
  },
})


